package com.example.usersvc.controller.v1;

import com.example.usersvc.domain.User;
import com.example.usersvc.domain.UserDto;
import com.example.usersvc.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("api/v1/users")
public class UserController {
    @Autowired
    private UserService userService;

    //@GetMapping
    //PATH: http://{host}:{port}/api/v1/users/{name}      -> @PathVariable String name 传参
    //Para: http://{host}:{port}/api/v1/users?name={name} -> @RequestParam("name") String name
    //RequestBody [Post] http://{host}:{port}/api/v1/users {"name":{}} ->@RequestBody LoginRequest name
    public Optional<User> getUser(@RequestParam("name") String name){
        return this.userService.findByName(name);
    }

    @GetMapping
    public List<User> getUsers() {
        return this.userService.findAll();
    }

    @PostMapping
    public User createUser(@RequestBody @Valid UserDto dto){
        return this.userService.save(dto);
    }

    @DeleteMapping
    public void deleteUser(@RequestParam("id") @NotBlank String id){
        this.userService.deleteById(id);
    }

}
